package com.xudow.app.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.xudow.app.R;

/**
 * 课程过滤弹出窗口
 *
 * @author lukecheng
 */
public class CourseFilterPopupWindow extends PopupWindow implements View.OnClickListener {
    private String searchtype;//查询类别1个人、2机构
    private String sorttype;//排序类别:1离我最近、2人气最高、3价格最低


    private double latitude;
    private double longitude;

    LocationClient locationClient;
    @Override
    public void onClick(View v) {
        if (selectTv!=null){
            selectIv.setVisibility(View.INVISIBLE);
            selectTv.setTextColor(Color.BLACK);
        }
        switch (v.getId()){
            case R.id.rl1:
                switchItem(defsort,dsIv);
                searchtype=null;
                sorttype=null;

                latitude=0;
                longitude=0;
                courseFilterListener.onSearchClick(searchtype, sorttype, latitude, longitude);
                break;
            case R.id.rl2:
                switchItem(nearsort,nsIv);
                searchtype=null;
                sorttype="1";
                locationClient=new LocationClient(context);
                //设置定位条件

                LocationClientOption option = new LocationClientOption();
                option.setScanSpan(0);
                option.setOpenGps(true);        //是否打开GPS

                option.setCoorType("bd09ll");       //设置返回值的坐标类型。

                option.setPriority(LocationClientOption.NetWorkFirst);  //设置定位优先级

                option.setProdName("LocationDemo"); //设置产品线名称。强烈建议您使用自定义的产品线名称，方便我们以后为您提供更高效准确的定位服务。

//        option.setScanSpan(5000);    //设置定时定位的时间间隔。单位毫秒

                locationClient.setLocOption(option);

                //注册监听
                final String[] s = {"s"};
                locationClient.registerLocationListener(new BDLocationListener() {
                    @Override
                    public void onReceiveLocation(BDLocation bdLocation) {

                    if(s[0].equals("s")){

                        longitude = bdLocation.getLongitude();
                        latitude = bdLocation.getLatitude();
                        Log.i("CourseFilterPopupWindow", "longitude1>>" + longitude + "longitude1>>>" + longitude);
                        courseFilterListener.onSearchClick(searchtype, sorttype, latitude, longitude);
                        s[0] ="x";
                    }


                    }
                });
                locationClient.start();
                locationClient.requestLocation();

                Log.i("CourseFilterPopupWindow", "longitude2>>" + longitude + "longitude>>>2" + longitude);

                if(locationClient!=null&&locationClient.isStarted()){

                    locationClient.stop();

                    locationClient=null;

                }
                break;
            case R.id.rl3:
                switchItem(hotsort,hsIv);
                searchtype=null;
                sorttype="2";
                latitude=0;
                longitude=0;
                courseFilterListener.onSearchClick(searchtype, sorttype, latitude, longitude);
                break;
            case R.id.rl4:
                switchItem(minpricesort, msIv);
                searchtype=null;
                sorttype="3";
                latitude=0;
                longitude=0;
                courseFilterListener.onSearchClick(searchtype, sorttype, latitude, longitude);
                break;
            case R.id.rl5:
                switchItem(agencysort,asIv);
                sorttype=null;
                searchtype="2";
                latitude=0;
                longitude=0;
                courseFilterListener.onSearchClick(searchtype, sorttype, latitude, longitude);
                break;
            case R.id.rl6:
                switchItem(teachsort,tsIv);
                searchtype="1";
                sorttype=null;
                latitude=0;
                longitude=0;
                courseFilterListener.onSearchClick(searchtype, sorttype, latitude, longitude);
                break;
        }

        dismiss();
    }

    /**
     * 选择item
     */
    private void switchItem(TextView tv,ImageView iv) {
        selectIv=iv;
        selectTv=tv;
        tv.setTextColor(Color.parseColor("#6db93d"));
        iv.setVisibility(View.VISIBLE);
    }

    public interface CourseFilterListener {

        /**
         * 筛选排序
         */
        void onSearchClick(String searchtype,String sorttype,double  latitude, double longitude);
    }

    private CourseFilterListener courseFilterListener;

    private Context context;

    private View contentView;

    private TextView defsort,nearsort,hotsort,minpricesort,agencysort,teachsort,selectTv;
    private ImageView dsIv,nsIv,hsIv,msIv,asIv,tsIv,selectIv;
    private RelativeLayout r1,r2,r3,r4,r5,r6;
    public CourseFilterPopupWindow(final Context context,CourseFilterListener listener) {
        super(context);

        this.context = context;
        courseFilterListener = listener;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.layout_course_filter_popup_window, null);
        initView();

        setContentView(contentView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setAnimationStyle(R.style.CourseFilterPopupAnimation);

        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.translucent));
        setBackgroundDrawable(dw);
    }

    private void initView() {
        r1=(RelativeLayout)contentView.findViewById(R.id.rl1);
        r2=(RelativeLayout)contentView.findViewById(R.id.rl2);
        r3=(RelativeLayout)contentView.findViewById(R.id.rl3);
        r4=(RelativeLayout)contentView.findViewById(R.id.rl4);
        r5=(RelativeLayout)contentView.findViewById(R.id.rl5);
        r6=(RelativeLayout)contentView.findViewById(R.id.rl6);
        defsort=(TextView)contentView.findViewById(R.id.defsort_tv);
        nearsort=(TextView)contentView.findViewById(R.id.nearsort_tv);
        hotsort=(TextView)contentView.findViewById(R.id.hotsort_tv);
        minpricesort=(TextView)contentView.findViewById(R.id.minpricesort_tv);
        agencysort=(TextView)contentView.findViewById(R.id.agencysort_tv);
        teachsort=(TextView)contentView.findViewById(R.id.teachsort_tv);
        dsIv=(ImageView)contentView.findViewById(R.id.defsort_iv);
        nsIv=(ImageView)contentView.findViewById(R.id.nearsort_iv);
        hsIv=(ImageView)contentView.findViewById(R.id.hotsort_iv);
        msIv=(ImageView)contentView.findViewById(R.id.minpricesort_iv);
        asIv=(ImageView)contentView.findViewById(R.id.agencysort_iv);
        tsIv=(ImageView)contentView.findViewById(R.id.teachsort_iv);
        r1.setOnClickListener(this);
        r2.setOnClickListener(this);
        r3.setOnClickListener(this);
        r4.setOnClickListener(this);
        r5.setOnClickListener(this);
        r6.setOnClickListener(this);
    }


}
